package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ulwx.database.DbException;
import com.ulwx.tool.CTime;
import com.ulwx.tool.DbUtils;
import feeinterface.modeldb.BaseSmsModemSelectTest;
import feeinterface.protocol.utils.Constants;

/**
 * 用于测试，根据指定的合作id选择对应的短信端口号(或手机号码)送给客户端
 * 
 * @author 黄杰
 * 
 */
public class BaseSmsModemSelectTestDao {

	/**
	 * 判断是否绑定了指定机型的销量统计，如果是，就返回指定的手机号码 2011-4-2
	 * 
	 * @param CoId
	 * @return
	 * @throws DbException
	 */
	public static String getSmsPortPhone(Integer CoId) throws Exception {
		BaseSmsModemSelectTest smsPortTest = null;
		String smsPortPhone = "";
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT BsSmsModemSelectTestSno,ServiceCoID,SmsCatport,Counter,Status,UpdateTime,Updator ");
		sb.append("FROM BaseSmsModemSelectTest WHERE ServiceCoID=? AND Counter < ? AND STATUS = 1         ");
		Map<Integer, Object> args = new HashMap<Integer, Object>();
		args.put(1, CoId);
		args.put(2, Constants.MobileLimit.MaxSentPhoneCnt);
		List<BaseSmsModemSelectTest> list = DbUtils.doQueryClass(null,
				BaseSmsModemSelectTest.class, sb.toString(), args);
		if (list != null && list.size() > 0) {
			smsPortTest = list.get(0);
			smsPortPhone = smsPortTest.getSmsCatport();
			// 如果有找到，就更新该手机号码的使用次数
			sb = new StringBuffer();
			sb.append("UPDATE BaseSmsModemSelectTest SET Counter = Counter + 1, STATUS = ? WHERE BsSmsModemSelectTestSno = ?");
			args.clear();
			if (smsPortTest.getCounter() >= Constants.MobileLimit.MaxSentPhoneCnt)
				smsPortTest.setStatus(0);
			if (CTime.datediff(CTime.getDate(), smsPortTest.getUpdateTime()) > 0)
				smsPortTest.setStatus(0);
			args.put(1, smsPortTest.getStatus());
			args.put(2, smsPortTest.getBsSmsModemSelectTestSno());
			DbUtils.executeBindUpdate(null, sb.toString(), args);
		}
		return smsPortPhone;
	}

	public static void main(String[] args) {
		try {
			// String phone =
			// ChannelSmsModemSelectTestDao.getSmsPortPhone(102221);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	//
}
